package fhp.hlhj.giantfold.module

import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.network.UrlApis
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.interfaces.ShowLoadingListener
import fhp.hlhj.giantfold.javaBean.ErroBean
import fhp.hlhj.giantfold.javaBean.GetKeyBean
import fhp.hlhj.giantfold.utils.LogUtil

/**
 * author: FHP
 * data: on 2017\12\4 0004 11:03
 * E-Mail: 103945089@qq.com
 */
class MainModule {
    interface IsOverListener{
        fun over(token:String)
        fun keyOver(getKeyBean: GetKeyBean)
        fun error(erroBean: ErroBean)
    }

    fun getKey(params: HttpParams,isOverListener: IsOverListener){
        UrlApis.getKey(params)
                .doOnSubscribe {

                }
                .subscribe {
                    data->
                    isOverListener.keyOver(data)
                }
    }

    /**
     * 获取token
     */
     fun getToken(isOverListener: IsOverListener, showLoading: ShowLoadingListener) {
        UrlApis.initTaoke(Urls.publicUrl+ Urls.getToken)
                .doOnSubscribe {
                    showLoading.showloading()
                }
                .subscribe({
                    data->
                    LogUtil.log("data是否为空"+(data==null))
                    isOverListener.over(data.data.token)
                },
                        {
                            t -> LogUtil.log("错误"+t.toString())
                            t.printStackTrace()
                            UrlApis.initTaokeStr(Urls.publicUrl+ Urls.getToken)
                                    .doOnSubscribe {
                                        showLoading.showloading()
                                    }
                                    .subscribe {
                                        str->
                                        LogUtil.log("二次获得"+str)
                                    }
                        })
    }
}